package com.hb.trade.server.project.file.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.project.file.dto.ReviewItemSecondDto;
import com.hb.core.pojo.project.file.entity.ReviewItemSecond;
import com.hb.core.pojo.project.file.mapper.ReviewItemSecondMapper;
import com.hb.core.pojo.project.file.mapper.ReviewItemThirdMapper;
import com.hb.trade.server.project.file.service.ReviewItemSecondService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fzzn.core.util.IdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 二级评审项 服务实现类
 * </p>
 *
 * @author tian
 * @since 2022-04-13
 */
@Service
public class ReviewItemSecondServiceImpl extends ServiceImpl<ReviewItemSecondMapper, ReviewItemSecond> implements ReviewItemSecondService {

    @Resource
    private ReviewItemThirdMapper reviewItemThirdMapper;

    /**
     * 查询二级评审项列表
     *
     * @param reviewItemSecondDto
     * @return org.fzzn.core.model.msg.Message<java.util.List < com.hb.core.pojo.project.file.dto.ReviewItemSecondDto>>
     * @author tian
     * @date 2022-4-13 11:26
     */
    @Override
    public List<ReviewItemSecondDto> selectReviewItemSecondList(ReviewItemSecondDto reviewItemSecondDto) {
        return baseMapper.selectReviewItemSecondByFirstReviewItemId(reviewItemSecondDto);
    }
    /**
     * 查询二级评审项列表(重载)
     * @author tian
     * @date 2022-4-13 11:26
     * @param firstReviewItemId:一级评审id
     * @return org.fzzn.core.model.msg.Message<java.util.List<com.hb.core.pojo.project.file.dto.ReviewItemSecondDto>>
     */
    @Override
    public List<ReviewItemSecondDto> selectReviewItemSecondList(Long firstReviewItemId) {
        ReviewItemSecondDto reviewItemSecondDto = new ReviewItemSecondDto();
        reviewItemSecondDto.setFirstReviewItemId(firstReviewItemId);
        return selectReviewItemSecondList(reviewItemSecondDto);
    }

    @Override
    public List<ReviewItemSecondDto> listReviewItemSecond(Long sectionId, Integer firstReviewItemType) {
        return baseMapper.listReviewItemSecond(sectionId,firstReviewItemType);
    }

    /**
     * 保存，编辑二级评审项
     *
     * @param reviewItemSecondDto
     * @return java.lang.Boolean
     * @author tian
     * @date 2022-4-13 11:30
     */
    @Override
    public Long editReviewItemSecond(ReviewItemSecondDto reviewItemSecondDto) {
        ReviewItemSecond reviewItemSecond = new ReviewItemSecond();
        reviewItemSecondDto.to(reviewItemSecond);
        Long id;
        if (null != reviewItemSecond.getId()) {
            reviewItemSecond.setUpdateId(reviewItemSecond.getCreateId());
            reviewItemSecond.setUpdateName(reviewItemSecond.getCreateName());
            reviewItemSecond.setUpdateTime(LocalDateTime.now());
            baseMapper.updateById(reviewItemSecond);
            id = reviewItemSecond.getId();
        } else {
            // 根据一级评审点查询最大的排序
            Integer sort = baseMapper.selectSortByFirstReviewItemId(reviewItemSecond.getFirstReviewItemId());
            id = IdUtil.getId();
            reviewItemSecond.setId(id);
            reviewItemSecond.setCreateId(reviewItemSecond.getCreateId());
            reviewItemSecond.setCreateName(reviewItemSecond.getCreateName());
            reviewItemSecond.setCreateTime(LocalDateTime.now());
            reviewItemSecond.setSort(sort + 1);
            baseMapper.insert(reviewItemSecond);
        }
        return id;
    }

    /**
     * 删除二级评审
     *
     * @param reviewItemSecondDto
     * @return java.lang.Boolean
     * @author tian
     * @date 2022-4-13 11:38
     */
    @Override
    @Transactional
    public Boolean deleteReviewItemSecond(ReviewItemSecondDto reviewItemSecondDto) {
        int i = baseMapper.deleteById(reviewItemSecondDto.getId());

        // 删除三级评审
        Map deleteMap = new HashMap();
        deleteMap.put("second_review_item_id", reviewItemSecondDto.getId());
        reviewItemThirdMapper.deleteByMap(deleteMap);
        return i > 0 ? true : false;
    }

    /**
     * 查询二级评审项是否已经汇总
     *
     * @author tian
     * @date 2022-5-20 14:33
     * @param secondReviewItemId
     * @return java.lang.Integer
     */
    @Override
    public Integer countBySecondReviewItemIdAndEvaluationStatus(Long secondReviewItemId, Long sectionId) {
        return baseMapper.countBySecondReviewItemIdAndEvaluationStatus(secondReviewItemId, sectionId);
    }
}
